package com.dg11185.nearshop.view;

import android.app.Dialog;
import android.content.Context;
import android.text.Html;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.nearshop.R;
import com.wlper.lib.sys.Screen;
import com.wlper.lib.uitls.ViewUtils;

/**
 * 自定义的AlertDialog
 * 
 * @author wanliang527
 * 
 */
public class MyAlertDialog extends Dialog {

    private Context mContext;

    private LayoutInflater mInflater;

    private TextView mTitleView;// 标题

    private TextView mMessageView;// 信息

    private Button mPosButton;// 确定按钮

    private Button mNegaButton;// 取消按钮

    public MyAlertDialog(Context context, int theme) {
        super(context, theme);
        init(context);
    }

    public MyAlertDialog(Context context) {
        super(context, R.style.wl_CustomDialog);
        init(context);
    }

    private void init(Context context) {
        mContext = context;
        mInflater = ViewUtils.getInflater(mContext);
        View v = mInflater.inflate(R.layout.view_custom_alert_dialog, null);
        // v.setLayoutParams(new LayoutParams((int) (Screen.getInstance(context)
        // .getScreenWidth() * 0.75), LayoutParams.WRAP_CONTENT));
        mTitleView = (TextView) v.findViewById(R.id.dialog_title);
        mMessageView = (TextView) v.findViewById(R.id.dialog_message);
        mPosButton = (Button) v.findViewById(R.id.btn_confirm);
        mNegaButton = (Button) v.findViewById(R.id.btn_cancel);
        this.setContentView(v);
        this.getWindow().getAttributes().width = (int) (Screen.getInstance(context).getScreenWidth() * 0.75);
        // this.setCanceledOnTouchOutside(false);
    }

    /**
     * 设置确定按钮
     * 
     * @param string
     * @param clickListener
     * @return
     */
    public MyAlertDialog setPositiveButton(String string, View.OnClickListener clickListener) {
        if (!mPosButton.getText().equals(string))
            mPosButton.setText(string);
        mPosButton.setOnClickListener(clickListener);
        mPosButton.setVisibility(View.VISIBLE);
        adjustMargin();
        return this;
    }

    /**
     * 设置取消按钮
     * 
     * @param string
     * @param clickListener
     * @return
     */
    public MyAlertDialog setNegativeButton(String string, View.OnClickListener clickListener) {
        if (!mNegaButton.getText().equals(string))
            mNegaButton.setText(string);
        mNegaButton.setOnClickListener(clickListener);
        mNegaButton.setVisibility(View.VISIBLE);
        adjustMargin();
        return this;
    }

    /**
     * 设置对话框标题
     * 
     * @param title
     * @return
     */
    public MyAlertDialog setTitle(String title) {
        if (!mTitleView.getText().equals(title))
            mTitleView.setText(title);
        return this;
    }

    /**
     * 设置对话框内容
     * 
     * @param message
     * @return
     */
    public MyAlertDialog setMessage(String message) {
        if (!mMessageView.getText().equals(message))
            mMessageView.setText(message);
        return this;
    }

    /**
     * 通过{@link Html #fromHtml(String)}设置对话框内容
     * 
     * @param message
     * @return
     */
    public MyAlertDialog setMessage(CharSequence message) {
        mMessageView.setText(message);
        mMessageView.setGravity(Gravity.LEFT); //设置左对齐
        return this;
    }

    private void adjustMargin() {
        if (mPosButton.getVisibility() == View.VISIBLE && mNegaButton.getVisibility() == View.VISIBLE) {
            ((LinearLayout.LayoutParams) mNegaButton.getLayoutParams()).leftMargin = 20;
        }
    }

}
